/*
 * This file is part of Cleanflight.
 *
 * Cleanflight is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cleanflight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cleanflight.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 Created by Dominic Clifton
  */
#include "display.h"
#include "max7456_symbols.h"
#include "osd_slave.h"
#include "displayport_max7456.h"
#include "vcd.h"
#include "systemTime.h"
#include "max7456.h"
#include "fontdata.h"


// when locked the system ignores requests to enter cli or bootloader mode via serial connection.
bool osdSlaveIsLocked = false;

static displayPort_t *osdDisplayPort;


static vcdProfile_t vcdProfile;

static void osdDrawLogo(int x, int y)
{
    uint16_t fontOffset = 160;
    for(uint8_t row=0;row<4;row++)
    {
        for (uint8_t column=0;column<24;column++)
        {
            if (fontOffset != 255)
            {
                displayWriteChar(osdDisplayPort, x + column, y + row, fontOffset++);
            }
        }
    }
}

bool receivingScreen = false;
bool stalled = false;

void osdSlaveClearScreen(void)
{
    displayClearScreen(osdDisplayPort);
    receivingScreen = true;
}

void osdSlaveWriteChar(const uint8_t x, const uint8_t y, const uint8_t c)
{
    displayWriteChar(osdDisplayPort, x, y, c);
}

void osdSlaveWrite(const uint8_t x, const uint8_t y, const char *s)
{
    displayWrite(osdDisplayPort, x, y, s);
}


void osdSlaveInit(displayPort_t *osdDisplayPortToUse)
{
    if (!osdDisplayPortToUse)
        return;

    osdDisplayPort = osdDisplayPortToUse;

    displayClearScreen(osdDisplayPort);

	displayWrite(osdDisplayPort, 0, 0, "\x64\x65");//红外
	
	displayWrite(osdDisplayPort, 0, 14, "\x6A\x6B----.------");//经度
	displayWrite(osdDisplayPort, 0, 15, "\x6C\x6D----.------");//纬度
	
	displayWrite(osdDisplayPort, 22, 14, "\x2----.-");//俯仰
	displayWrite(osdDisplayPort, 22, 15, "\x3----.-");//方位
	
    displayResync(osdDisplayPort);

}


/*
 * Called periodically by the scheduler
 */
void osdSlaveUpdate(void)
{
    displayDrawScreen(osdDisplayPort);
}


// 向7456写入字库文件
void WriteMax7456FontData(){
    for(uint8_t i=0;i<254;i++){
        max7456WriteNvm(i,&fontdata[i*64]);
    }
    displayReInit(osdDisplayPort);
}


// 显示所有OSD字符
void ShowAllOsdChar(){

    displayClearScreen(osdDisplayPort);
    uint16_t fontOffset = 0;
    for(uint8_t row=0;row<16;row++)
    {
        for (uint8_t column=0;column<30;column++)
        {
            if (fontOffset < 480)
            {
                displayWriteChar(osdDisplayPort, 0 + column, 0 + row, fontOffset++);
            }
        }
    }
    displayResync(osdDisplayPort);
}


void OsdFuncInit(void){
    vcdProfile.video_system = VIDEO_SYSTEM_PAL;
    vcdProfile.h_offset = 0;
    vcdProfile.v_offset = 0;
    // 硬件函数接口会通过这个函数交给osdDisplayPort
    osdDisplayPort = max7456DisplayPortInit(&vcdProfile);
    // 绘制Logo 显示版本号等信息
    osdSlaveInit(osdDisplayPort); 

    // 第一次使用需要将下面两行打开，向osd芯片写入字符集，然后重启即可
//	WriteMax7456FontData();
//	ShowAllOsdChar();

}

void OsdFuncLoop(void){
//    osdSlaveUpdate();
//	max7456RefreshAll( );
    
    BlinkLogo();

}


// func函数，用于测试功能


// logo显示状态反转
void LogoToggle(void){
    static bool logoState = false;
    if (logoState){
        displayClearScreen(osdDisplayPort);
        logoState = !logoState;
    }else{
        osdSlaveInit(osdDisplayPort); 
        logoState = !logoState;
    }

}



void BlinkLogo(void){
    static uint32_t lastBlinkTime = 0;
    if (millis() - lastBlinkTime > 500){
        LogoToggle();
        lastBlinkTime = millis();
    }
}


